<?php require ('setcookie.html'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="de">
<head>
    <title>Lingomoto - Passwort vergessen - Sprachen lernen</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="Lingomoto - Passwort vergessen - Sprachen lernen" />
    <meta name="language" content="de" />
    <link href="style.css" type="text/css" rel="stylesheet" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
</head>

<body>
<?php
// read configuration
require ('konfiguration.html');
// read user-data
require ('nutzer.html');
// read header
$link = "passwort-vergessen.html";
$langen = 'forgot-password.html';
$langes = 'olvidado-password.html';
$langru = 'forgot-password.html';
require ('kopf.html');
// <!-- horizontal column -->
echo '<div>';
require ('horizontal.html');
echo '</div>';
// <!-- Left Column -->
echo '<div id="left_col">';
require ('vertikal.html');
echo '</div>';


// <!-- Content -->

echo '<div id="content">';
echo '<div id="block5">';

// initialize variables
$email = isset($_REQUEST['email']) ? $_REQUEST['email'] : '';

if ( $email == '' ) 
{
    show_lostform();
}
else
{
    change_lostpassword();

    if ( $num_rows == 1 ) 
    {
        send_email();
    }
    else
    {
        show_lostform();
    }    
}


// ----------------------- Functions --------------------------------------------------------------------------------------------

function show_lostform() {

    global $email;

    echo '<form action="passwort-vergessen.html" method="post" >';
    echo '<h2>Neues Passwort</h2>';
    echo "<br />";
    echo '<div class="content_frame">';
    echo 'Ein neues Passwort wird an Deine E-Mail-Adresse gesendet:';
    echo "<br />";
    echo "<br />";
    echo '<ul>';
    echo '<li>E-Mail:</li>';
    echo '<li><input type="text" name="email" size="25" maxlength="60" class="input" onClick=this.value="" value="';
    // Content, if form was not completed
    echo $email;
    echo '">';
    echo '<li><input type="image" src="bilder/buttons/senden.gif" name="email" alt="senden" title=" Senden " /></li>';
    echo '</ul>';
    echo "</div>";
    echo '</form>';
}

function change_lostpassword() {

    global $password,$num_rows,$email,$newpassword;

    echo '<form action="passwort-vergessen.html" method="post" >';
    
    // check, if already exists
	$sql = " select password from customerdata where email = '$email' ";

    // DB Query
    $db_sel = mysql_query( $sql );
    if ( ! $db_sel ) { die('Error query: ' . mysql_error()); }

    $num_rows = mysql_num_rows($db_sel);
    if ( $num_rows == 1 ) {

    // create new password
    $length = 8;
    $dummy = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'), array('-','+'));
    // shuffle array
    mt_srand((double)microtime()*1000000);
    for ($i = 1; $i <= (count($dummy)*2); $i++)
    {
        $swap = mt_rand(0,count($dummy)-1);
        $tmp = $dummy[$swap];
        $dummy[$swap] = $dummy[0];
        $dummy[0] = $tmp;
    }
    // get password
    $newpassword = substr(implode('',$dummy),0,$length);
  
    // convert password
    $password = md5($newpassword);
    $newpa = 1;


    // update
	$sql1 = " update customerdata set password ='$password', newpassword =". $newpa . ", tries = 0, customerip ='". $_SERVER['REMOTE_ADDR'] . "' where email = '$email' ";

    $db_upd = mysql_query( $sql1 );
    if ( ! $db_upd ) { die('Error query: ' . mysql_error()); }
    
    }
    else
    {
    echo '<div class="message">Die Eingabe ist ung&uuml;ltig! Bitte &uuml;berpr&uuml;fe die E-Mail.</div>';
    echo "<br />";
    }    

    echo '</form>';
}

function send_email() {

    global $email,$newpassword;
    
    $mailtext = "Dein neues Passwort für Lingomoto lautet: ";
    $mailtext .= "\r\n";
    $mailtext .= "\r\n";
    $mailtext .= $newpassword;
    $mailtext .= "\r\n";
    $mailtext .= "\r\n";

    $receiver = $email;
    $sender   = "info@lingomoto.com";
    $subject  = "Dein Passwort";
    $replyto  = "info@lingomoto.com";

mail( $receiver,
      $subject,
      $mailtext,
      "From: Lingomoto <". $sender . ">\nReply-To: $replyto",
      "-f $sender");

    echo '<h2>E-Mail wurde verschickt</h2>';

}

echo '</div>';
echo '</div>';
echo '<div id="adverts">';
echo '<div id="adbox"></div>';
echo '</div>';

require ('fuss.html');
?>

</body>
</html>